﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CFM.API;
using CFM.DTO;
using CFM.Common;
using System.Text.RegularExpressions;


namespace CFM
{
    public partial class frmUser : Form
    {
        private UserAPI _api = null;
        private RoleAPI _apiRole = null;
        public frmUser()
        {
            InitializeComponent();
            
        }
        private void frmUser_Load(object sender, EventArgs e)
        {
            _api = new UserAPI();
            _apiRole = new RoleAPI();
            grvNguoidung.AutoGenerateColumns = false;
            EnableControl(false);
           
            btnLuu.Enabled = false;
            btnSua.Enabled = false;
            btnXoa.Enabled = false;

            BindDataGridView();
            LoadcbbRoleID();

           
        }
        private void BindDataGridView()
        {
            grvNguoidung.DataSource = _api.GetAllUser();
        }
        private void btnThem_Cilck(object sender, EventArgs e)
        {
            EnableControl(true);
            ResetControl();
                     

            btnXoa.Enabled = false;
            btnSua.Enabled = false;
            btnLuu.Enabled = true;
        }
        private void grvNguoidung_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0)
                return;
            User user = grvNguoidung.Rows[e.RowIndex].DataBoundItem as User;        
            txtEmail.Text = user.Email;
            txtFullName.Text = user.FullName;
            txtPassword.Text = user.Password;
            txtPhone.Text = user.Phone;
            txtUserID.Text = user.UserId;
            cbxRoleId.SelectedValue = user.RoleId.Trim();
            EnableControl(false);
            MethodHelper.ClearErrorProvider();

            btnLuu.Enabled = false;
            btnSua.Enabled = true;
            btnXoa.Enabled = true;           
        }    
        private void ResetControl()
        {
             txtUserID.Text = string.Empty;
             txtEmail.Text = string.Empty;
             txtFullName.Text = string.Empty;
             txtPassword.Text = string.Empty;
             txtPhone.Text = string.Empty;             
             btnXoa.Enabled = false;
             btnLuu.Enabled = true;
             btnXoa.Enabled = false;           
             
            MethodHelper.ClearErrorProvider();
        }
        private void EnableControl(bool flag)
        {
            txtUserID.Enabled = flag;
            txtFullName.Enabled = flag;
            txtPassword.Enabled = flag;
            txtPhone.Enabled = flag;
            txtEmail.Enabled = flag;
            cbxRoleId.Enabled = flag;
        }

        private void txtUserID_TextChanged(object sender, EventArgs e)
        {
            if (txtUserID.Enabled == true)
            {
                txtUserID.Text = MethodHelper.GetIdFromName(txtUserID.Text);
            }
        }
        private void LoadcbbRoleID()
        {
            //cbbRoleId.Items.Clear();
            cbxRoleId.DataSource = _apiRole.GetAll();
            cbxRoleId.DisplayMember = "RoleName";
            cbxRoleId.ValueMember = "RoleId";             
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (txtUserID.Text.Length > 0)
            {
                EnableControl(true);
                txtUserID.Enabled = false;
                MethodHelper.ClearErrorProvider();

                //cbbRoleId.Items.Clear();
                btnLuu.Enabled = true;
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (txtUserID.Text.Length > 0)
            {
                _api.Delete(txtUserID.Text.Trim());
                BindDataGridView();
                ResetControl();

                btnXoa.Enabled = false;
                btnSua.Enabled = false;
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (!CheckBeforeSave())
                return;
            User user = new User();
            user.UserId = txtUserID.Text;
            
            user.FullName = txtFullName.Text;
            user.Password = txtPassword.Text;
            user.Email = txtEmail.Text;
            user.Phone =  txtPhone.Text;
            user.RoleId = Convert.ToString(cbxRoleId.SelectedValue);
            user.UpdateDate = DateTime.Now;
            if (txtUserID.Enabled)
                _api.Create(user);
            else
                _api.Update(user);
            BindDataGridView();
            ResetControl();
            MethodHelper.ClearErrorProvider();

            btnXoa.Enabled = false;
            btnSua.Enabled = false;
            
        }
        private bool CheckBeforeSave()
        {
            bool flag = true;
            if (txtUserID.Enabled && txtUserID.Text.Length == 0)
            {
                MethodHelper.AddErrorProvider(txtUserID, Const.EMPTY_MESSAGE);
                flag = false;
            }
            if (txtPassword.Enabled && txtPassword.Text.Length == 0)
            {
                MethodHelper.AddErrorProvider(txtPassword, Const.EMPTY_MESSAGE);

                flag = false;
            }
            if (txtPassword2.Enabled && txtPassword.Text.Length == 0)
            {
                MethodHelper.AddErrorProvider(txtPassword2, Const.EMPTY_MESSAGE);

                flag = false;
            }
            if (txtFullName.Enabled && txtFullName.Text.Length == 0)
            {
                MethodHelper.AddErrorProvider(txtFullName, Const.EMPTY_MESSAGE);

                flag = false;
            }
            if (txtEmail.Enabled && txtEmail.Text.Length == 0)
            {
                MethodHelper.AddErrorProvider(txtEmail, Const.EMPTY_MESSAGE);

                flag = false;
            }
            if (txtPhone.Enabled && txtPhone.Text.Length == 0)
            {
                MethodHelper.AddErrorProvider(txtPhone, Const.EMPTY_MESSAGE);

                flag = false;
            }
            if (txtPassword.Text !=txtPassword2.Text)
            {
                MethodHelper.AddErrorProvider(txtPassword2, " Mật khẩu phải trung nhau");

                flag = false;
            }         
             
            //if (!txtUnitName.Enabled && !txtUnitId.Enabled)
           //     flag = false;
            return flag;
        }

        private void txtPhone_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && !Char.IsPunctuation(e.KeyChar))
                e.Handled = true;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
